﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Data.OracleClient;
using System.Text;
using ReflectionStudio.Core.Events;
namespace WPFExcelReport
{
    public class GB12Data : BaseFunction
    {
        #region -------------function -------------------------
        public int ThreadCount;
        public GB12Data()
            : base()
        {

        }
        public GB12Data(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        ~GB12Data()
        {
            if (OracleDB != null)
            {
                OracleDB.Close();
            }
        }
        public DataSet GetABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();

                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                or1.CommandText = "SELECT DISTINCT ABPAUFKRS,ABPAUFLFD,ABPAUFPOS,ABPART, ABPVAR, ABPFARBE,0 as ABPRESMGE,P.ABPERLKZ,P.ABPERLDAT,"
                    + "GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7,GSAGARNNR AS HLTNUM"
                + " FROM  ABPOS P JOIN GAAUF F ON P.ABPAUFART IN ('WB','PR') AND F.GAAUFKRS=P.ABPAUFKRS AND F.GAAUFLFD=P.ABPAUFLFD AND F.GAAUFPOS=P.ABPAUFPOS"
                + " LEFT JOIN GSDAT B ON F.GAMANDANT = B.GSMANDANT AND F.GAART = B.GSART AND F.GAVAR = B.GSVAR "
                + " WHERE ABPMANDANT='00'  AND ABPERLKZ=:ABPERLKZ"
                + " ORDER BY  1,2,3";
                ;
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();

                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GAKG1");
                    Q100.Enqueue("GAKG2");
                    Q100.Enqueue("GAKG3");
                    Q100.Enqueue("GAKG4");
                    Q100.Enqueue("GAKG5");
                    Q100.Enqueue("GAKG6");
                    Q100.Enqueue("GAKG7");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["ABPFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["ABPFARBE"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["GAFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["GAFARBE"] = value;
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public DataSet GetVWABPOS(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                OracleCommand or1 = OracleDB.CreateCommand();

                string ABPAUFKRS = dwABPOS["ABPAUFKRS"].ToString();
                string ABPAUFLFD = dwABPOS["ABPAUFLFD"].ToString();
                string ABPAUFPOS = dwABPOS["ABPAUFPOS"].ToString();
                or1.CommandText = "SELECT DISTINCT P.ABPAUFART,W.ABPAUFKRS,W.ABPAUFLFD,W.ABPAUFPOS,W.ABPART,0 as ABPRESMGE,"
                    + "W.ABPVAR, W.ABPFARBE, GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7"
                    + "  FROM  ABPOS P JOIN GAAUF F ON P.ABPAUFART IN ('VW') AND F.GAAUFKRS=P.ABPAUFKRS"
                    + " AND F.GAAUFLFD=P.ABPAUFLFD AND F.GAAUFPOS=P.ABPAUFPOS"
                + " RIGHT JOIN ABAUFM MU ON P.ABPAUFKRS=MU.ABAAUFKRS"
                    + " AND P.ABPAUFLFD=MU.ABAAUFLFD AND P.ABPAUFPOS=MU.ABAAUFPOS"
                    + " RIGHT JOIN ABPOS W ON W.ABPAUFKRS=MU.ABAVKAUFKRS AND W.ABPAUFLFD=MU.ABAVKAUFLFD AND W.ABPAUFPOS=MU.ABAVKAUFPOS "
                + " WHERE P.ABPMANDANT='00'  AND P.ABPERLKZ=:ABPERLKZ"
                + " AND GAKG3>0 AND GAKG5=0"
                + " ORDER BY  1,2,3";
                or1.Parameters.Add("ABPERLKZ", OracleType.NVarChar).Value = dwABPOS["ABPERLKZ"].ToString();

                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABPOS";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GAKG1");
                    Q100.Enqueue("GAKG2");
                    Q100.Enqueue("GAKG3");
                    Q100.Enqueue("GAKG4");
                    Q100.Enqueue("GAKG5");
                    Q100.Enqueue("GAKG6");
                    Q100.Enqueue("GAKG7");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["ABPFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["ABPFARBE"] = value;
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["GAFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["GAFARBE"] = value;

                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        public override DataSet GetVKPOS(DataRow dwABPOS)
        {
            return base.GetVKPOS(dwABPOS);
        }
        private DataSet GetYARNUSAGE(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region source code
                /*
                  SELECT DISTINCT P.ABPAUFKRS as SBAUFKRS,P.ABPAUFLFD AS SBAUFLFD,P.ABPAUFPOS AS SBAUFPOS,LSLIEFNA,LSNAME1 AS SPRODUCER
FROM ABPOS P LEFT JOIN ABAUFM M ON P.ABPAUFART ='VW' AND P.ABPAUFKRS=M.ABAAUFKRS AND P.ABPAUFLFD=M.ABAAUFLFD AND P.ABPAUFPOS=M.ABAAUFPOS
LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS LEFT JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR WHERE   M.ABAVKAUFKRS='PR' AND M.ABAVKAUFLFD='100428' AND M.ABAVKAUFPOS='010'
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT DISTINCT P.ABPAUFKRS||P.ABPAUFLFD||P.ABPAUFPOS AS YUORDER"
                    + ",P.ABPAUFART AS YUORDERTYPE,ABARESQTY AS MUQUANTITY"
                    + ",LSLIEFNA,LSNAME1 AS TPRODUCER"
                + "  FROM ABAUFM M LEFT JOIN ABPOS P ON P.ABPAUFKRS=M.ABAVKAUFKRS AND P.ABPAUFLFD=M.ABAVKAUFLFD AND P.ABPAUFPOS=M.ABAVKAUFPOS"
                + " LEFT JOIN ABFOLGE F ON  P.ABPAUFKRS=ABFAUFKRS AND P.ABPAUFLFD= ABFAUFLFD AND P.ABPAUFPOS=F.ABFAUFPOS"
                + " JOIN LIEFDAT L ON  F.ABFLIEFNR=L.LSLIEFNR AND F.ABFMANDANT =L.LSMANDANT "
                + " WHERE  M.ABAAPOS>'0001' AND P.ABPAUFART in('DI','PR','VW')  "
                    + " WHERE M.ABAAUFKRS=:ABPAUFKRS AND M.ABAAUFLFD=:ABPAUFLFD AND M.ABAAUFPOS=:ABPAUFPOS";
                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ABFOLGE";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("MUQUANTITY");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["TPRODUCER"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["TPRODUCER"] = value;
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetMATYARNINFO(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                /*
                  SELECT GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7 FROM ABPOS P LEFT JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS
WHERE GAAUFKRS='PR' AND  GAAUFLFD='100428' AND GAAUFPOS='010'
                 */
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = "SELECT GAART,GAVAR,GAFARBE,GAKG1,GAKG2,GAKG3,GAKG4,GAKG5,GAKG6,GAKG7"
                + " FROM ABPOS P LEFT JOIN GAAUF F ON P.ABPAUFKRS=F.GAAUFKRS AND P.ABPAUFLFD=F.GAAUFLFD AND P.ABPAUFPOS=F.GAAUFPOS"
                + " WHERE GAAUFKRS=:ABPAUFKRS AND  GAAUFLFD=:ABPAUFLFD AND GAAUFPOS=:ABPAUFPOS";

                or1.Parameters.Add("ABPAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABPAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABPAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "GAAUF";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("GAKG1");
                    Q100.Enqueue("GAKG5");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["GAFARBE"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["GAFARBE"] = value;
                    }
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        private DataSet GetZUTEI(DataRow dwABPOS)
        {
            DataSet set4Return = new DataSet();
            DataSet setLocal = new DataSet();
            DataTable tableLocal = null;
            try
            {
                #region code reference
                /*
                  SELECT DISTINCT LSLIEFNA,LSNAME1,ABFMNRKRS||ABFMLFDNR||ABFMPOS AS GLOOMNUM FROM ABFOLGE F,LIEFDAT L  WHERE ABFMANDANT='00'
AND   F.ABFAUFKRS='PR' AND  ABFAUFLFD='100428' AND  ABFAUFPOS='010'  AND F.ABFLIEFNR=L.LSLIEFNR
                 */
                #endregion
                OracleCommand or1 = OracleDB.CreateCommand();
                or1.CommandText = " SELECT DISTINCT GPSPPART,ZUEINTKG,ZUNEUDAT"
                + " FROM ZUTEI F LEFT JOIN GPDAT  ON ZUPARTIE=GPPARTIE"
                + " WHERE ZUMANDANT='00'  AND  ZUAUFKRS=:ABFAUFKRS AND ZUAUFLFD=:ABFAUFLFD AND ZUAUFPOS=:ABFAUFPOS ";

                or1.Parameters.Add("ABFAUFKRS", OracleType.NVarChar).Value = dwABPOS["ABPAUFKRS"].ToString();
                or1.Parameters.Add("ABFAUFLFD", OracleType.NVarChar).Value = dwABPOS["ABPAUFLFD"].ToString();
                or1.Parameters.Add("ABFAUFPOS", OracleType.NVarChar).Value = dwABPOS["ABPAUFPOS"].ToString();
                setLocal = Connect2Proxy(or1);

                if (setLocal.Tables.Count > 0)
                {
                    tableLocal = setLocal.Tables[0];
                    tableLocal.TableName = "ZUTEI";
                    Queue Q100 = new Queue();
                    Q100.Enqueue("ZUEINTKG");
                    DivideBy100(tableLocal, Q100);
                    byte[] b;
                    string value;
                    string sTemp = "", GPSPPART = "", ZUEINTKG = "", ZUNEUDAT = "";
                    foreach (DataRow dwTemp in tableLocal.Rows)
                    {
                        b = Encoding.GetEncoding("ISO-8859-1").GetBytes(dwTemp["GPSPPART"].ToString().Trim());
                        value = Encoding.GetEncoding("gb2312").GetString(b);
                        dwTemp["GPSPPART"] = value;
                        //GPSPPART,ZUEINTKG,ZUNEUDAT
                        sTemp = value + ";";
                        if (!GPSPPART.Contains(sTemp)
                             && sTemp != ";")
                        {
                            GPSPPART += sTemp;
                        }
                        dwABPOS["ZUEINTKG"] = dwTemp["ZUEINTKG"];
                        sTemp = dwTemp["ZUEINTKG"].ToString().Trim() + ";";
                        if (!ZUEINTKG.Contains(sTemp)
                             && sTemp != ";")
                        {
                            ZUEINTKG += sTemp;
                        }
                        dwABPOS["ZUNEUDAT"] = dwTemp["ZUNEUDAT"];
                    }
                    GPSPPART += "***";
                    GPSPPART = GPSPPART.Replace(";***", "");
                    GPSPPART = GPSPPART.Replace("***", "");
                    GPSPPART = GPSPPART.Replace(";", Environment.NewLine);
                    dwABPOS["GPSPPART"] = GPSPPART;
                    ZUEINTKG += "***";
                    ZUEINTKG = ZUEINTKG.Replace(";***", "");
                    ZUEINTKG = ZUEINTKG.Replace("***", "");
                    ZUEINTKG = ZUEINTKG.Replace(";", Environment.NewLine);
                    dwABPOS["ZUEINTKG"] = ZUEINTKG;
                    Tracer.Verbose(ABPOS + " 纱批", GPSPPART + ":" + ZUEINTKG);
                    Tracer.Speak(ABPOS + " 纱批"+ GPSPPART + ":" + ZUEINTKG);
                    set4Return.Merge(setLocal);
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex); ;
            }
            return set4Return;
        }
        #endregion
        public override void BuildABPOS(DataRow dwABPOS)
        {
            DataRow dwNABPOS = dwABPOS.Table.Clone().NewRow();
            dwNABPOS.ItemArray = dwABPOS.ItemArray;
            DataSet set4Return = new DataSet();
            ABPOS = dwNABPOS["ABPAUFKRS"].ToString() + "." + dwNABPOS["ABPAUFLFD"].ToString() + "." + dwNABPOS["ABPAUFPOS"].ToString();
            ABPOS = ThreadCount.ToString() + ": " + ABPOS;
            try
            {
                string ABAVKAUFTYP = "";
                string LSNAME1 = "";
                string sTemp = "";

                #region----------------------- 查询客单----------------------------
                GetVKPOS(dwNABPOS);
                #endregion

                #region----------------------- 查询纱批----------------------------
                GetZUTEI(dwNABPOS);

                #endregion
            }
            catch (Exception ex)
            {
                Tracer.Error(ABPOS + ex.StackTrace, ex);
            }
            finally
            {
                Tracer.Verbose(ABPOS, "完成");
                try
                {
                    dwABPOS.ItemArray = dwNABPOS.ItemArray;
                    string progress = "查询进展:[" + ThreadID.ToString() + "] 完成<-FinishedJobs->/<-TotalJobs->";
                    EventDispatcher.Instance.RaiseStatus(progress, StatusEventType.StartProgress);
                }
                catch (Exception ex)
                {
                    Tracer.Error(ABPOS + ex.StackTrace, ex);
                }
                //set4Return = ImportDataTable(set4Return, dwNABPOS);
            }
        }
    }
}